package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {
    @Autowired
    StrategyMapper strategyMapper;


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Strategy> list = strategyMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Strategy entity) {
        if(entity.getId()!=null){
            strategyMapper.updateByPrimaryKey(entity);
        }else{
            strategyMapper.insert(entity);
        }
    }

    @Override
    public List<Strategy> listAll() {
        return strategyMapper.selectAll();
    }

    @Override
    public Strategy getById(Long id) {
        return strategyMapper.selectByPrimaryKey(id);
    }

    /**
     * index.html 的查询攻略的方法
     * @param qo 关键字,标题,副标题
     * @return 单个结果
     */
    @Override
    public List<Strategy> queryForStrategy(QueryObject qo) {
        return strategyMapper.selectForStrategy(qo);
    }

    @Override
    public void updateLikeNum(Long relationId) {
        strategyMapper.updateLikeNum(relationId);
    }

    /**
     * 取消点赞的业务方法
     * @param relationId
     */
    @Override
    public void updateByDeleteLikeNum(Long relationId) {
        strategyMapper.updateByDeleteLikeNum(relationId);
    }

}
